package cn.zqh.router.controller;



import cn.hutool.core.lang.id.NanoId;
import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.bo.AddANewRouter;
import cn.zqh.bean.bo.AdminChangeRouter;
import cn.zqh.router.annotation.RepeatSubmit;
import cn.zqh.router.service.RouterService;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zqh
 * @since 2022-12-22
 */
@Api("路由管理")
@RestController
@RequestMapping("/router")
public class RouterController {

    @Autowired
    private RouterService routerService;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;




    /**
      * @Author: zqh
      * @Date:
      * @Description: 添加一个新路由
    */
    @ApiOperation(value = "添加一个新路由")
    @PostMapping("addANewRouter")
    public CommonResult addANewRouterInfo(@RequestBody AddANewRouter addANewRouter){
        return routerService.addANewRouterInfo(addANewRouter);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 修改某个路由的信息
    */
    @ApiOperation(value = "修改某个路由的信息")
    @PostMapping("modifyARouteInformation")
    public CommonResult modifyARouteInformation(@RequestBody AdminChangeRouter adminRouter){
        return routerService.modifyARouteInformation(adminRouter);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询路由
    */
    @ApiOperation(value = "通过id查询路由")
    @GetMapping("getRouterById")
    public CommonResult getRouterById(Integer routeId){
        return routerService.getRouterById(routeId);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询所有路由
    */
    @ApiOperation(value = "查询所有路由")
    @GetMapping("getAllRouter")
    public CommonResult getAllRouter(){
        return routerService.getAllRouter();
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 根据id删除路由
    */
    @ApiOperation(value = "根据id删除路由")
    @DeleteMapping("DelRouterById")
    public CommonResult delRouterById(Integer routeId){
        return routerService.delRouterById(routeId);
    }

    /**
      * @Author: zqh
      * @Date:
      * @Description: 查询已经删除了的路由
    */
    @ApiOperation(value = "查询已经删除了的路由")
    @GetMapping("getALlDelRouter")
    public CommonResult getDelRouter(){
        return routerService.getDelRouter();
    }


    /**
     * @Author: zqh
     * @Date:
     * @Description: 检查qq验证码是否正确
     */
    @PostMapping("/checkQQCode")
    public CommonResult checkQQCode(String email, String qQCode) {
        String code = (String) redisTemplate.opsForValue().get(email);
        System.out.println(code);
        if (code == null || "".equals(code)){
            return CommonResult.failed("验证码错误，或已失效");
        }
        if (!code.equals(qQCode)){
            return CommonResult.failed("验证码错误，或已失效");
        }
        return CommonResult.success(null);
    }

    /**
     * @Author: zqh
     * @Date:
     * @Description: 创建qq验证码
     */
    @RepeatSubmit(respond = "")
    @GetMapping("/getQqCode")
    public CommonResult getQqCode(String email) {

        String qQCode = NanoId.randomNanoId(6);
//        String resCode = NanoId.randomNanoId(6);
//        redisService.setCacheObject(qQCode, resCode, 240L, TimeUnit.SECONDS);
        redisTemplate.opsForValue().set(email,qQCode,240L, TimeUnit.SECONDS);
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("email",email);
        hashMap.put("code",qQCode);
        String s = JSON.toJSONString(hashMap);
        rabbitTemplate.convertAndSend("ms2","",s);
        HashMap<String, String> stringStringHashMap = new HashMap<>();
        stringStringHashMap.put("验证码", qQCode);
        return CommonResult.success(stringStringHashMap, "验证码发送成功,4分钟内有效");
    }
    









}

